import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class Monitor extends JFrame {
	private static final long serialVersionUID = 1L;

	Client client;

	JLabel speed;
	JLabel thread;
	JLabel totalUsers;
	JLabel totalTweets;
	JLabel totalTraffic;
	JLabel runningTime;
	JButton plus;
	JButton minus;
	JButton stop;
	
	public void setupMonitor() {
		client = new Client(this);

		this.setSize(320, 150);
		this.setLayout(new GridLayout(7,1));
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JLabel label;
		Container container;
		
		// Speed display
		container = new Container();
		container.setLayout(new FlowLayout());
		speed = new JLabel("0 tweets/sec");
		speed.setFont(new Font("Serif", Font.BOLD, 30));
		speed.setForeground(Color.red);
		container.add(speed);
		this.add(container);
		
		// Threads display
		container = new Container();
		container.setLayout(new FlowLayout());
		label = new JLabel("Max Threads:");
		thread = new JLabel(String.valueOf(client.maxPageFetcherThreads));
		thread.setForeground(Color.red);
		plus = new JButton("+");
		plus.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            	client.increaseMaxPageFetcherThreads();
            	thread.setText(client.getMaxPageFetcherThreads()+"");
            }
        });
		minus = new JButton("-");
		minus.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            	client.decreaseMaxPageFetcherThreads();
            	thread.setText(client.getMaxPageFetcherThreads()+"");
            }
        });
		container.add(label);
		container.add(thread);
		container.add(plus);
		container.add(minus);
		this.add(container);
		
		// Total users
		container = new Container();
		container.setLayout(new BorderLayout());
		label = new JLabel("Total users fetched:");
		totalUsers = new JLabel("0 user");
		totalUsers.setForeground(Color.red);
		container.add(label, BorderLayout.WEST);
		container.add(totalUsers, BorderLayout.EAST);
		this.add(container);
		
		// Total tweets
		container = new Container();
		container.setLayout(new BorderLayout());
		label = new JLabel("Total tweets fetched:");
		totalTweets = new JLabel("0 tweet");
		totalTweets.setForeground(Color.red);
		container.add(label, BorderLayout.WEST);
		container.add(totalTweets, BorderLayout.EAST);
		this.add(container);
		
		// Total traffic
		container = new Container();
		container.setLayout(new BorderLayout());
		label = new JLabel("Total traffic");
		totalTraffic = new JLabel("0 bytes");
		totalTraffic.setForeground(Color.red);
		container.add(label, BorderLayout.WEST);
		container.add(totalTraffic, BorderLayout.EAST);
		this.add(container);
		
		// Total running time
		container = new Container();
		container.setLayout(new BorderLayout());
		label = new JLabel("Running time:");
		runningTime = new JLabel("0 second");
		runningTime.setForeground(Color.red);
		container.add(label, BorderLayout.WEST);
		container.add(runningTime, BorderLayout.EAST);
		this.add(container);

		// State button
		stop = new JButton("Start");	
		stop.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            	if(!client.isAlive()) {
            		client.start();
            	}
            	else {
            		client.toggleState();
            	}
            }
        });
		this.add(stop);

		// Make visible
		this.pack();
		this.setVisible(true);
	}
}
